package com.zklh.book.system.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户获取bean的接口
 */
public class BeanFactory {

    private static Map<Class, Object>  beanMap = new HashMap<>();

    public static <T> T getBean(Class<T> clazz) {
        if (beanMap.containsKey(clazz)) {
            return (T) beanMap.get(clazz);
        } else {
            try {
                beanMap.put(clazz, clazz.newInstance());
                return (T) beanMap.get(clazz);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
